package com.huaxiafinance.management.service.impl;

import com.google.common.collect.Maps;
import com.huaxiafinance.management.constant.CommonConstant;
import com.huaxiafinance.management.dto.request.MemberDto;
import com.huaxiafinance.management.dto.request.PassWhitelistDto;
import com.huaxiafinance.management.dto.request.QuerFroUserListDto;
import com.huaxiafinance.management.mapper.*;
import com.huaxiafinance.management.model.*;
import com.huaxiafinance.management.service.YfqFrontUserService;
import com.huaxiafinance.management.util.DateUtil;
import com.huaxiafinance.management.util.MessageResult;
import com.huaxiafinance.management.util.ServiceCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by lenovo on 2017/5/9.
 */
@Service("yfqFrontUserService")
@SuppressWarnings("SpringJavaAutowiringInspection")
public class YfqFrontUserServiceImpl implements YfqFrontUserService {
    @Autowired
    private FrontUserMapper frontUserMapper;
    @Autowired
    private ScoreGradeMapper scoreGradeMapper;
    @Autowired
    private MemberInfoMapper memberInfoMapper;
    @Autowired
    private MallScoreRecordMapper mallScoreRecordMapper;
    @Autowired
    private UserShippingAddressMapper userShippingAddressMapper;
    @Autowired
    private HandleLogMapper handleLogMapper;


    @Override
    public List<Map<String, Object>> queryList(QuerFroUserListDto dto) {
        dto.setPage(dto.getPage() == null || dto.getRows() == null ? null : dto.getPage() * dto.getRows());
        List<Map<String, Object>> m = null;
        m = frontUserMapper.queryList(dto);
        return m;
    }

    @Override
    public int countList(QuerFroUserListDto dto) {
        int i = 0;
        //为了app 和H5渠道
        if ("0".equals(dto.getSourceFrom()) || "-1".equals(dto.getSourceFrom())) {
            //3.1版本已没有用到
            i = frontUserMapper.countListAH5(dto);
        } else {
            i = frontUserMapper.countList(dto);
        }
        return i;
    }

    @Override
    public Map queryInformation(String userUuid) {
        Map map = new HashMap();
        Map user = frontUserMapper.selectByUserUuid(userUuid);
        if(user == null){
            return map;
        }
        //createTime
        map.put("createTime",user.get("created_at"));
        //macCode
        map.put("macCode",user.get("mac_code"));
        //name
        map.put("name",user.get("real_name"));
        //cellphone
        map.put("cellphone",user.get("cellphone"));
        //idCard
        map.put("idCard",user.get("id_card"));


        //注册来源壹分期和壹纷趣
        int type = 0;
        if(user.get("type") != null){
            type = (int)user.get("type");
        }
        switch ((type)) {
            case 1:
                map.put("type", "壹分期");
                break;
            case 2:
                map.put("type", "壹纷趣");
                break;
            default:
                break;
        }

        return map;
    }

    /**
     * 请求积分商城会员等级列表
     *
     * @return 会员列表
     */
    @Override
    public MessageResult queryScoreGrade() throws Exception {
        MessageResult messageResult = new MessageResult();
        List<ScoreGrade> scoreGradeList = scoreGradeMapper.queryScoreGrade();
        messageResult.setSuccessResult();
        messageResult.setData(scoreGradeList);
        return messageResult;
    }

    /**
     * 请求会员详情以及会员机会的增删详情列表
     *
     * @param dto 请求参数
     * @return 会员详情以及积分列表增删详情列表
     */
    @Override
    public MessageResult queryMemberDetails(MemberDto dto) throws Exception {
        MessageResult messageResult = new MessageResult();
        if (dto.getUserUuid() == null) {
            messageResult.setFailureResult("请重新请求");
            return messageResult;
        }
        //获取月初时间
        Date startDate = DateUtil.getBeginMonthTime();
        dto.setStartDate(startDate);
        MemberInfo memberInfo = memberInfoMapper.queryMemberDetails(dto);
        //分页查询
        if (dto.getRows()==null||dto.getPage()==null){
            dto.setPage(CommonConstant.PAGE);
            dto.setRows(CommonConstant.ROWS);
        }else {
            dto.setPage(dto.getPage() * dto.getRows());
        }
        List<MallScoreRecord> mallScoreRecordList = new ArrayList<MallScoreRecord>();
        int countGradeSum = 0;
        //计算时间
        if (dto.getStartTime() != null && dto.getEndTime() != null
                &&!("").equals(dto.getStartTime())
                &&!("").equals(dto.getEndTime())) {
            Map<String, Date> mapTime = DateUtil.getTimeFormat(dto.getStartTime(), dto.getEndTime());
            dto.setBeginTime(mapTime.get("beginTime"));
            dto.setOverTime(mapTime.get("overTime"));
            mallScoreRecordList = mallScoreRecordMapper.selectByMemDto(dto);
            countGradeSum = mallScoreRecordMapper.countGrade(dto);
        }
        if (memberInfo == null) {
            memberInfo = new MemberInfo();
        }
        memberInfo.setMallScoreRecordList(mallScoreRecordList);
        messageResult.setData(memberInfo);
        messageResult.setRows(countGradeSum);
        messageResult.setSuccessResult();
        return messageResult;
    }

    /**
     * 请求会员等级详情以及会员积分增加详情列表
     *
     * @param dto 请求参数
     * @return 请求会员等级详情以及会员积分增加详情列表
     */
    @Override
    public MessageResult queryMembershipLevel(MemberDto dto) throws Exception {
        MessageResult messageResult = new MessageResult();
        if (dto.getUserUuid() == null) {
            messageResult.setFailureResult("请重新请求");
            return messageResult;
        }
        MemberInfo memberInfo = memberInfoMapper.queryMembershipLevel(dto);
        //分页查询
        if (dto.getRows()==null||dto.getPage()==null){
            dto.setPage(CommonConstant.PAGE);
            dto.setRows(CommonConstant.ROWS);
        }else {
            dto.setPage(dto.getPage() * dto.getRows());
        }
        int n = 0;
        List<MallScoreRecord> mallScoreRecordList = new ArrayList<MallScoreRecord>();
        //计算时间
        if (dto.getStartTime() != null && dto.getEndTime() != null
                &&!("").equals(dto.getStartTime())
                &&!("").equals(dto.getEndTime())) {
            Map<String, Date> mapTime = DateUtil.getTimeFormat(dto.getStartTime(), dto.getEndTime());
            dto.setBeginTime(mapTime.get("beginTime"));
            dto.setOverTime(mapTime.get("overTime"));
            mallScoreRecordList = mallScoreRecordMapper.queryMembershipAddLevel(dto);
            n = mallScoreRecordMapper.countAddLevel(dto);
        }
        if (memberInfo == null) {
            memberInfo = new MemberInfo();
        }
        memberInfo.setMallScoreRecordList(mallScoreRecordList);
        messageResult.setData(memberInfo);
        messageResult.setRows(n);
        messageResult.setSuccessResult();
        return messageResult;
    }

    /**
     * 请求用户收货地址列表
     *
     * @param dto 请求参数
     * @return 返回用户地址列表
     */
    public MessageResult queryUserAddress(MemberDto dto) throws Exception {
        MessageResult messageResult = new MessageResult();
        if (dto.getUserUuid() == null) {
            messageResult.setFailureResult("请重新请求");
            return messageResult;
        }
        if (dto.getRows()==null||dto.getPage()==null){
            dto.setPage(CommonConstant.PAGE);
            dto.setRows(CommonConstant.ROWS);
        }else {
            dto.setPage(dto.getPage() * dto.getRows());
        }
        List<UserShippingAddress> userShippingAddressList = userShippingAddressMapper.selectByUserUuid(dto);
        messageResult.setData(userShippingAddressList);
        dto.setPage(null);
        List<UserShippingAddress> countList = userShippingAddressMapper.selectByUserUuid(dto);
        messageResult.setRows(countList.size());
        messageResult.setSuccessResult();
        return messageResult;
    }

    @Transactional
    @Override
    public MessageResult passWhitelist(PassWhitelistDto dto) throws Exception {
        MessageResult messageResult = new MessageResult();
        FrontUserDetail userDetail = new FrontUserDetail();
        userDetail.setMessage(dto.getMessage());
        userDetail.setAuthTime(new Date());
        userDetail.setUserUuId(dto.getUserUuid());
        int i = frontUserMapper.passWhitelist(userDetail);

        if (i > 0){
            HandleLog hl = new HandleLog();
            hl.setHandleType("passWhitelist");
            hl.setHandleMessage("{userUuid=" + dto.getUserUuid() + ",adminUserId=" + dto.getAdminUserId() + ",message=" + dto.getMessage() + "}");
            i = handleLogMapper.insert(hl);

            if (i > 0)
                messageResult.setResultMessage(ServiceCode.SUCCESS);
            else
                messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);
        }

        return messageResult;
    }

    @Override
    public MessageResult blackOrWhite(String userUuid, Integer isBlack) throws Exception {
        MessageResult messageResult = new MessageResult();

        Map<String, Object> map = Maps.newHashMap();

        map.put("userUuid", userUuid);
        map.put("isBlack", isBlack);

        int i = frontUserMapper.blackOrWhite(map);

        if (i > 0)
            messageResult.setResultMessage(ServiceCode.SUCCESS);
        else
            messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);

        return messageResult;
    }

}
